package com.web.base;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.beanutils.BeanUtils;

public class Model implements java.io.Serializable {

	/**
     *
     */
	private static final long serialVersionUID = 1L;
	private Integer id;

	public Model(Serializable serializable) {
		Model _Model = (Model) serializable;
		this.setId(_Model.getId());
		this.setId_string(_Model.getId_string());

	}

	public Model() {

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * 添加了一个 字符转格式的主健
	 */
	private String id_string;

	/**
	 * make object as json
	 * 
	 * @return
	 */
	public String toJson() {
		try {
			// e.x {"UserID":11, "Name":"Truly", "Email":"zuac◎hotmail.com"}
			Map<Object, Object> map;
			map = BeanUtils.describe(this);
			Iterator<Object> keyIt = map.keySet().iterator();
			StringBuilder builder = new StringBuilder("{ ");
			int i = 0;
			while (keyIt.hasNext()) {
				String key = (String) keyIt.next();
				if ("class".equals(key)) {
					continue;
				}
				Object value = map.get(key);
				if (i > 0) {
					builder.append(", ");
				}
				builder.append("'" + key + "' : '" + value + "'");
				i++;
			}
			builder.append(" }");
			return builder.toString();
		} catch (IllegalAccessException ex) {
			return "";
		} catch (InvocationTargetException ex) {
			return "";
		} catch (NoSuchMethodException ex) {
			return "";
		}

	}

	public String toString() {
		try {
			Map<Object, Object> map;
			map = BeanUtils.describe(this);

			Iterator<Object> keyIt = map.keySet().iterator();
			StringBuilder builder = new StringBuilder(this.getClass().getName()
					+ ": [ ");
			int i = 0;
			while (keyIt.hasNext()) {
				String key = (String) keyIt.next();

				if ("class".equals(key)) {
					continue;
				}
				Object value = map.get(key);

				if (i > 0) {
					builder.append(", ");
				}

				builder.append(key + " = " + value + "");
				i++;
			}
			builder.append(" ]");
			return builder.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return super.toString();
		}
	}

	/**
	 * 判断所有属性是否为空
	 * 
	 * @return
	 */
	public boolean empty() {

		try {
			Map<Object, Object> map;
			map = BeanUtils.describe(this);
			Iterator<Object> keyIt = map.keySet().iterator();

			int i = 0;
			while (keyIt.hasNext()) {
				String key = (String) keyIt.next();

				if ("class".equals(key)) {
					continue;
				}
				Object value = map.get(key);

				if (value != null) {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		return true;
	}

	public String getId_string() {
		return id_string;
	}

	public void setId_string(String id_string) {
		this.id_string = id_string;
	}

	public static void main(String[] args) {

		Model _Model = new Model();
		System.out.print(_Model);
		System.out.print(_Model.empty());
		_Model.setId(1);
		System.out.print(_Model.empty());
	}
}
